home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mep1 / External Functions / Samples / ProIcon.h next >
Encoding:
C/C++ Source or Header  |  1990-09-28  |  26.4 KB  |  396 lines  |  [TEXT/KAHL]

  1. /*
  2.  * ProIcon.h - include file defining ProIcon internal data structures.
  3.  *
  4.  * Used when creating external functions to be loaded by ProIcon.
  5.  *
  6.  * For use with Think C 4.0 and ProIcon 2.0.
  7.  */
  8. #ifndef NULL
  9. #define NULL 0L
  10. #endif
  11.  
  12. #define extended double                /* ProIcon uses Think C's 80-bit reals */
  13.  
  14. typedef long int word;                /* ProIcon signed word        */
  15. typedef unsigned long int uword;    /* ProIcon unsigned word    */
  16. typedef void *pointer;                /* general pointer            */
  17. typedef struct descrip *dptr;        /* pointer to descriptor    */
  18. typedef unsigned short int DIGIT;    /* large-integer digit        */
  19.  
  20. #define IntBits        16                /* number of bits in a Think C integer    */
  21. #define BitOffMask    (IntBits-1)
  22. #define LogIntBits    4                /* log of IntBits */
  23. #define MaxCvtLen    257                /* largest string in conversions; the extra */
  24.                                      /*  one is for a terminating null */
  25. #define MaxInt        077777            /* largest Think C int */
  26. #define MaxStrLen    0777777777        /* maximum string length */
  27. #define MaxUnsigned ((unsigned short int)0177777)    /* largest unsigned Think C int */
  28. #define WordSize    sizeof(word)    /* number of bytes in a ProIcon word */
  29. #define WordBits    32                /* number of bits in a ProIcon word */
  30. #define LogWordSize 2                /* log of WordSize */
  31. #define DescSize    sizeof(struct descrip) /* number of bytes in a descriptor */
  32. #define LogDescSize 3                /* log base 2 of DescSize */
  33.  
  34. #define CsetSize    (256/IntBits)    /* number of ints to hold 256 cset bits    */
  35. #define CStateSize    15                /* size of C state for co-expressions    */
  36. #define ActStkBlkEnts    100            /* number of entries in an astkblk        */
  37. #define HSlots        4                /* Initial number of hash buckets in tables and sets    */
  38. #define LogHSlots     2                /* log base 2 of HSlots                    */
  39. #define HSegs         6                /* Maximum number of hash bin segments    */
  40.  
  41. /*
  42.  * Codes returned by runtime support routines.
  43.  *  Note, some conversion routines also return type codes.
  44.  */
  45. #define Less      -1
  46. #define Equal       0
  47. #define Greater       1
  48. #define CvtFail      -2
  49. #define Cvt          -3
  50. #define NoCvt      -4
  51. #define Failure      -5
  52. #define Defaulted -6
  53. #define Success      -7
  54. #define Error      -8
  55.                                     /* number of hash bins is HSlots * 2 ^ (HSegs - 1).        */
  56. /*
  57.  * Type codes (descriptors and blocks).
  58.  */
  59. #define T_Null         0                /* null value */
  60. #define T_Integer     1                /* integer */
  61. #define T_Bignum     2                /* long integer */
  62. #define T_Real         3                /* real number */
  63. #define T_Cset         4                /* cset */
  64. #define T_File         5                /* file */
  65. #define T_Proc         6                /* procedure */
  66. #define T_List         7                /* list header */
  67. #define T_Table         8                /* table header */
  68. #define T_Record     9                /* record */
  69. #define T_Telem        10                /* table element */
  70. #define T_Lelem        11                /* list element */
  71. #define T_Tvsubs    12                /* substring trapped variable */
  72. #define T_Tvkywd    13                /* keyword trapped variable */
  73. #define T_Tvtbl        14                /* table element trapped variable */
  74. #define T_Set        15                /* set header */
  75. #define T_Selem        16                /* set element */
  76. #define T_Refresh    17                /* refresh block */
  77. #define T_Coexpr    18                /* co-expression */
  78. #define T_External    19                /* external block */
  79. #define T_Slots        20                /* set/table hash slots */
  80.  
  81. /*
  82.  * Descriptor types and flags in descriptor type word.
  83.  */
  84. #define F_Nqual     0x80000000        /* set if NOT string qualifier */
  85. #define F_Var        0x40000000        /* set if variable */
  86. #define F_Tvar        0x20000000        /* set if trapped variable */
  87. #define F_Ptr        0x10000000        /* set if value field is pointer */
  88.  
  89. #define D_Null        (word)(T_Null | F_Nqual)
  90. #define D_Integer    (word)(T_Integer | F_Nqual)
  91. #define D_Bignum    (word)(T_Bignum | F_Ptr | F_Nqual)
  92. #define D_Real        (word)(T_Real | F_Ptr | F_Nqual)
  93. #define D_Cset        (word)(T_Cset | F_Ptr | F_Nqual)
  94. #define D_File        (word)(T_File | F_Ptr | F_Nqual)
  95. #define D_Proc        (word)(T_Proc | F_Ptr | F_Nqual)
  96. #define D_List        (word)(T_List | F_Ptr | F_Nqual)
  97. #define D_Table        (word)(T_Table | F_Ptr | F_Nqual)
  98. #define D_Telem        (word)(T_Telem | F_Ptr | F_Nqual)
  99. #define D_Tvsubs    (word)(T_Tvsubs | D_Tvar)
  100. #define D_Tvkywd    (word)(T_Tvkywd | D_Tvar)
  101. #define D_Tvtbl        (word)(T_Tvtbl | D_Tvar)
  102. #define D_Record    (word)(T_Record | F_Ptr | F_Nqual)
  103. #define D_Set        (word)(T_Set | F_Ptr | F_Nqual)
  104. #define D_Refresh    (word)(T_Refresh | F_Ptr | F_Nqual)
  105. #define D_Coexpr    (word)(T_Coexpr | F_Ptr | F_Nqual)
  106. #define D_External    (word)(T_External | F_Ptr | F_Nqual)
  107. #define D_Slots        (word)(T_Slots | F_Ptr | F_Nqual)
  108. #define D_Var        (word)(F_Var | F_Nqual | F_Ptr)
  109. #define D_Tvar        (word)(D_Var | F_Tvar)
  110. #define TypeMask    63    /* type mask */
  111. #define OffsetMask    (~(D_Tvar)) /* offset mask for variables */
  112.  
  113. /*
  114.  * Error codes returnable through *ip that produce the textual
  115.  * message shown with each.
  116.  */
  117. #define Errnone    -1)\
  118.                / sizeof(word))
  119.  
  120. /*
  121.  * Offset in word of cset bit.
  122.  */
  123. #define CsetOff(b)    ((b) & BitOffMask) 
  124. /*
  125.  * Address of word of cset bit.
  126.  */
  127. #define CsetPtr(b,c)    ((c) + (((b)&0377) >> LogIntBits)) 
  128. /*
  129.  * Set bit b in cset c.
  130.  */
  131. #define Setb(b,c)    (*CsetPtr(b,c) |= (01 << CsetOff(b))) 
  132. /*
  133.  * Test bit b in cset c.
  134.  */
  135. #define Testb(b,c)    ((*CsetPtr(b,c) >> CsetOff(b)) & 01) 
  136.  
  137. /* determine the number of words needed for a bignum block with n digits */
  138.  
  139. #define BigNeed(n)   ( ((sizeof(struct b_bignum) + ((n) - 1) * sizeof(DIGIT)) \
  140.                + WordSize - 1) & -WordSize )
  141.  
  142. /*
  143.  * Macros to access Icon arguments in C functions.
  144.  */
  145.  
  146. /*
  147.  * n-th argument.
  148.  */
  149. #define Arg(n)         (dargv[n])
  150.  
  151. /*
  152.  * Specific arguments.
  153.  */
  154. #define Arg0    (dargv[0])
  155. #define Arg1    (dargv[1])
  156. #define Arg2    (dargv[2])
  157. #define Arg3    (dargv[3])
  158. #define Arg4    (dargv[4])
  159. #define Arg5    (dargv[5])
  160. #define Arg6    (dargv[6])
  161.  
  162. /*
  163.  * Type field of n-th argument.
  164.  */
  165. #define ArgType(n)    (dargv[n].dword)
  166.  
  167. /*
  168.  * Value field of n-th argument.
  169.  */
  170. #define ArgVal(n)    (dargv[n].vword.integr)
  171.  
  172.  
  173. typedef union {                        /* object code operation, operand    */
  174.    short int *op;
  175.    word *opnd;
  176.    } inst;
  177.  
  178. /*
  179.  * ProIcon descriptor.
  180.  */
  181. struct descrip {
  182.    word dword;                        /*   type field                        */
  183.    union {                            /*   value field, one of:            */
  184.       word integr;                    /*     integer value                */
  185.       char *sptr;                    /*     pointer to character string    */
  186.       union block *bptr;            /*     pointer to a block            */
  187.       dptr descptr;                    /*     pointer to a descriptor        */
  188.       } vword;
  189.    };
  190.  
  191. /*
  192.  * ProIcon string qualifier.
  193.  */
  194. struct sdescrip {
  195.    word length;                        /*   length of string                */
  196.    char *string;                    /*   pointer to string                */
  197.    };
  198.  
  199. /*
  200.  * Frame markers
  201.  */
  202. struct ef_marker {                    /* expression frame marker        */
  203.    inst ef_failure;                    /*   failure ipc                */
  204.    struct ef_marker *ef_efp;        /*   efp                        */
  205.    struct gf_marker *ef_gfp;        /*   gfp                        */
  206.    word ef_ilevel;                    /*   ilevel                        */
  207.    };
  208.  
  209. struct pf_marker {                    /* procedure frame marker        */
  210.    word pf_nargs;                    /*   number of arguments        */
  211.    struct pf_marker *pf_pfp;        /*   saved pfp                    */
  212.    struct ef_marker *pf_efp;        /*   saved efp                    */
  213.    struct gf_marker *pf_gfp;        /*   saved gfp                    */
  214.    dptr pf_argp;                    /*   saved argp                    */
  215.    inst pf_ipc;                        /*   saved ipc                    */
  216.    word pf_ilevel;                    /*   saved ilevel                */
  217.    dptr pf_scan;                    /*   saved scanning environment    */
  218.    struct descrip pf_locals[1];        /*   descriptors for locals        */
  219.    };
  220.  
  221. struct gf_marker {                    /* generator frame marker        */
  222.    word gf_gentype;                    /*   type                        */
  223.    struct ef_marker *gf_efp;        /*   efp                        */
  224.    struct gf_marker *gf_gfp;        /*   gfp                        */
  225.    inst gf_ipc;                        /*   ipc                        */
  226.    struct pf_marker *gf_pfp;        /*   pfp                        */
  227.    dptr gf_argp;                    /*   argp                        */
  228.    };
  229.  
  230. /*
  231.  * Blocks
  232.  */
  233. struct b_bignum {                    /* large integer block */
  234.    word title;                        /*   T_Bignum */
  235.    word blksize;                    /*   block size */
  236.    word msd, lsd;                    /*   most and least significant digits */
  237.    short int sign;                    /*   sign; 0 positive, 1 negative */
  238.    DIGIT digits[1];                    /*   digits */
  239.    };
  240.  
  241. struct b_real {                        /* real block */
  242.    word title;                        /*   T_Real */
  243.    extended realval;                /*   value */
  244.    };
  245.  
  246. struct b_cset {                        /* cset block */
  247.    word title;                        /*   T_Cset */
  248.    word size;                        /*   size of cset */
  249.    short int bits[CsetSize];        /*   array of bits */
  250.    };
  251.  
  252. struct b_file {                        /* file block */
  253.    word title;                        /*   T_File */
  254.    struct pifile *fd;                /*   ProIcon file structure */
  255.    word status;                        /*   file status */
  256.    struct descrip fname;            /*   file name (string qualifier) */
  257.    };
  258.                                     /* status flags b_file status word */
  259. #define Fs_Read         01                /*   read access */
  260. #define Fs_Write     02                /*   write access */
  261. #define Fs_Create     04                /*   file created on open */
  262. #define Fs_Append    010                /*   append mode */
  263. #define Fs_Pipe        020                /*   reading/writing on a pipe */
  264. #define Fs_Record   040             /*   record structured file */
  265. #define Fs_Reading 0100             /*   last file operation was read */
  266. #define Fs_Writing 0200      /* allocate a cset block */
  267.    T_alcextrnl,            /* allocate an external block */
  268.    T_alcfile,            /* allocate a file block */
  269.    T_alchash,            /* allocate a hashed structure (set or table header) */
  270.    T_alclist,            /* allocate a list header block */
  271.    T_alclstb,            /* allocate a list element block */
  272.    T_alcreal,            /* allocate a real value block */
  273.    T_alcrecd,            /* allocate record with nflds fields */
  274.    T_alcrefresh,        /* allocate a co-expression refresh block */
  275.    T_alcsegment,        /* allocate a slot block */
  276.    T_alcselem,            /* allocate a set element block */
  277.    T_alcstr,            /* allocate a string in the string space */
  278.    T_alcsubs,            /* allocate a substring trapped variable */
  279.    T_alctelem,            /* allocate a table element block */
  280.    T_alctvtbl,            /* allocate a table element trapped variable block */
  281.    T_blkreq,            /* insure at least n bytes left in the block region */
  282.    T_cvint,                /* convert the value at dp into an integer */
  283.    T_cvreal,            /* convert the value at dp to a real in place */
  284.    T_cvstr,                /* convert dp (in place) into a string, using sbuf as buffer */
  285.    T_emptystr,            /* return pointer to descriptor for empty string */
  286.    T_makereal,            /* make a real number descriptor and associated block */
  287.    T_nulldesc,            /* return pointer to descriptor for null value */
  288.    T_onedesc,            /* return pointer to descriptor for integer one */
  289.    T_qtos,                /* convert a qualified string named by *dp to a C-style string */
  290.    T_strreq,            /* insure at least n bytes left in the string space */
  291.    T_zerodesc            /* return pointer to descriptor for integer zero */
  292.    };
  293.  
  294. #define Fail         {return NULL;}
  295. #define Max(x,y)     ((x)>(y)?(x):(y))            /* Maximum of x and y. */
  296. #define Min(x,y)     ((x)<(y)?(x):(y))            /* Minimum of x and y. */
  297. #define Return         {return &Arg0;}
  298. #define RunErr(n,dp) {*ip = n; return dp;}
  299.  
  300. pointer callback(int callno, ...);
  301.  
  302. /*
  303.  * callbacks in interface version 1
  304.  */
  305.  
  306. /* interface version number */
  307. #define vcallback() ((word)(callback(T_vers)))
  308.  
  309. /* allocate a co-expression activation block in the block region */
  310. #define alcactiv() ((struct astkblk *)callback(T_alcactiv))
  311.  
  312. /* allocate an n-digit bignum in the block region */
  313. #define alcbignum(n) ((struct b_bignum *)callback(T_alcbignum, (word)n))
  314.  
  315. /* allocate a co-expression stack block in the block region */
  316. #define alccoexp() ((struct b_coexpr *)callback(T_alccoexp))
  317.  
  318. /* allocate a cset in the block region */
  319. #define alccset() ((struct b_cset *)callback(T_alccset))
  320.  
  321. /* allocate an n-word external block in the block region */
  322. #define alcextrnl(n) ((struct b_external *)callback(T_alcextrnl, (uword)n))
  323.  
  324. /* allocate a file block in the block region */
  325. #define alcfile(fd, status, name) ((struct b_file *)callback(T_alcfile, (struct pifile *)fd, (short int)status, (dptr)name))
  326.  
  327. /* allocate a hashed structure (set or table header) in the block region */
  328. #define alchash(tcode) ((union block *)callback(T_alchash, (int)tcode))
  329.  
  330. /* allocate a list header block in the block region */
  331. #define alclist(size) ((struct b_list *)callback(T_alclist, (uword)size))
  332.  
  333. /* allocate a list element block in the block region */
  334. #define alclstb(nslots, first, nused) ((struct b_lelem *)callback(T_alclstb, (uword)nslots, (uword)first, (uword)nused))
  335.  
  336. /* allocate a real value in the block region */
  337. #define alcreal(val) ((struct b_real *)callback(T_alcreal, (extended)val))
  338.  
  339. /* allocate record with nflds fields in the block region */
  340. #define alcrecd(nflds, recptr) ((struct b_record *)callback(T_alcrecd, (short int)nflds, (union block **)recptr))
  341.  
  342. /* allocate a co-expression refresh block in the block region */
  343. #define alcrefresh(entryx, na, nl) ((struct b_refresh *)callback(T_alcrefresh, (word *)entryx, (short int)na, (short int)nl))
  344.  
  345. /* allocate a slot block in the block region */
  346. #define alcsegment(nslots) ((struct b_slots *)callback(T_alcsegment, (word)nslots))
  347.  
  348. /* allocate a set element block in the block region */
  349. #define alcselem(mbr, hn) ((struct b_selem *)callback(T_alcselem, (dptr)mbr, (uword)hn))
  350.  
  351. /* allocate an slen-character string in the string region, copy s to it */
  352. #define alcstr(s, slen) ((char *)callback(T_alcstr, (char *)s, (word)slen))
  353.  
  354. /* allocate a substring trapped variable in the block region */
  355. #define alcsubs(len, pos, var) ((struct b_tvsubs *)callback(T_alcsubs, (word)len, (word)pos, (dptr)var))
  356.  
  357. /* allocate a table element block in the block region */
  358. #define alctelem() ((struct b_telem *)callback(T_alctelem))
  359.  
  360. /* allocate a table element trapped variable block in the block region */
  361. #define alctvtbl(tbl, ref, hashnum) ((struct b_tvtbl *)callback(T_alctvtbl, (dptr)tbl, (dptr)ref, (uword)hashnum))
  362.  
  363. /* insure that at least bytes of space are left in the block region */
  364. #define blkreq(n) ((short int)callback(T_blkreq, (uword)n))
  365.  
  366. /* convert dp (in place) to integer */
  367. #define cvint(dp) ((short int)callback(T_cvint, (dptr)dp))
  368.  
  369. /* convert dp (in place) to real */
  370. #define cvreal(dp) ((short int)callback(T_cvreal, (dptr)dp))
  371.  
  372. /* convert dp (in place) into a string, using sbuf as buffer */
  373. #define cvstr(dp, sbuf) ((short int)callback(T_cvstr, (dptr)dp, (char *)sbuf))
  374.  
  375. /* make a real number descriptor and associated block in the block region */
  376. #define makereal(val, dp) ((short int)callback(T_makereal, (extended)val, (dptr)dp))
  377.  
  378. /* convert a qualified string to a C-style string, using sbuf as buffer */
  379. #define qtos(dp, sbuf) ((short int)callback(T_qtos, (dptr)dp, (char *)sbuf))
  380.  
  381. /* insure that at least n bytes of space are left in the string space */
  382. #define strreq(n) ((short int)callback(T_strreq, (uword)n))
  383.  
  384. /*
  385.  * descriptor pointers provided as callbacks
  386.  */
  387. #define Emptydp ((dptr)callback(T_emptystr))    /* pointer to empty string descriptor */
  388. #define Nulldp ((dptr)callback(T_nulldesc))        /* pointer to null value descriptor */
  389. #define Onedp ((dptr)callback(T_onedesc))        /* pointer to integer one descriptor */
  390. #define Zerodp ((dptr)callback(T_zerodesc))        /* pointer to integer zero descriptor */
  391.  
  392. /*
  393.  * end of callbacks in interface version 1
  394.  */
  395.  
  396.